/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_LOG_
#define	_INC_AK_LOG_

#include "ak_types.h"
#include "ak_queue.h"
#include "ak_lock.h"
#include "ak_clk.h"
#include "ak_mem.h"
#include "ak_socket.h"
#include "ak_gears.h"
#include "ak_vfs.h"

#ifdef __cplusplus
extern "C" {
#endif


#define _S_LOG_MAX_ANCHOR 		0X05
#define _S_LOG_MAX_CTRL 		0X02

/* Type define of the Log Anchor, Each has a queue of Log Elements. It's the Real Process Unit 
** In the Log Client's Recording Service.
*/
typedef struct _t_log_anchor{
_t_bnode      	node;
_t_blist 	    queue;
int 			index;
int 			reserv;
}_t_log_anchor;


typedef struct _t_log_mon{
	int 				iFile;
	int 				iCount;
	int 				iOffset;
	FILE 			 * 	pFileI;	/* Index */
	FILE 			 * 	pFileM;	/* Monitor */
	char 				filePath[S_APP_MAX_PATH];
}_t_log_mon;


struct _t_log_task;

typedef struct _t_log_ctrl{
	int 				bkEnd;
	int 				reserv;

	/* Elem Pool */
	_t_sysMHeap 		mHeap;
	_t_sysMPool 		mPool;
	_t_sysMSlot 		mSlot;

	unsigned int 		eSize;
	unsigned int 		eTotal;
	unsigned int 		eThrs;

	/* Anchor */
	_t_sysCS 			lock;
	_t_blist 	     	aFreeQ;
	_t_blist  			aActvQ;
	_t_log_anchor 	*	pAnchor;
	_t_log_anchor 		anchorAU[_S_LOG_MAX_ANCHOR];

	/* Task */
	struct _t_log_task* pTask;
	
	/* File Handle */
	int 				iFile;
	int 				iCount;
	int 				iOffset;
	FILE 			 * 	pFileI;	/* Index */
	FILE 			 * 	pFileM;	/* Monitor */
	FILE 			 * 	pFileA;	/* Ascii */
	FILE 			 * 	pFileB;	/* Binary */
	char 				filePath[S_APP_MAX_PATH];

	/* Socket Handle */
	int 				bNet;
	int 				sock;
	_u_ipport 			ipPort;
	
	/* Function */
	void 			 (* funcRec)();
}_t_log_ctrl;


typedef struct _t_log_task{
	/* The Recording Process Queue */
	_t_sysSemB 				wake;
	_t_sysTask 				task;
	_t_sysTaskAttr 			attr;
	int 					timer;

	/* The Log Element Controller */
	_t_log_ctrl 			ctrl[_S_LOG_MAX_CTRL];
}_t_log_task;


/* Logging Type Support :
** Ascii
** Binary
*/
typedef enum {
	_S_LOG_T_ASCII  = 0X00,
	_S_LOG_T_BINARY = 0X01,
	_S_LOG_T_MFILE 	= 0X02,
	_S_LOG_T_MNET	= 0X03,
	_S_LOG_T_MAX 	= 0X04
}_e_log_type;

#define SLOG_TASK_CTRL_N(logTask, num) 	((logTask)->ctrl + (num))


/* Record BackEnd */
#define _S_LOG_BKEND_NUL  	0X00
#define _S_LOG_BKEND_STD  	0X01
#define _S_LOG_BKEND_FILE 	0X02
#define _S_LOG_BKEND_NET 	0X03
#define _S_LOG_BKEND_DB  	0X04



#define _S_LOG_SVR_PORT 	9900


/* Logging Function Interface:
**
*/
extern	int 	SLog_Init(_t_log_task * pTask);
extern	int 	SLog_Exec(_t_log_task * pTask);

extern	int 	SLog_SetBkEnd(_t_log_ctrl* pCtrl,  int bkEnd, const char * filePath, unsigned int ip, unsigned short port);
extern	int 	SLogging(_t_log_ctrl 	* pCtrl,	int type,
						const char  * sFunc, 	int nLine,
						char 		* pDesc, 	int descLen,
						char 		* pData, 	int dataLen);

/* Logging Mon Interface:
**
*/
extern	int 	SMon_Init(_t_log_mon * pMon, const char * filePath);
extern	int 	SMon_Fini(_t_log_mon * pMon);
extern	int 	SMon_Show(_t_log_mon * pMon, int eIdx, unsigned char * pBuf, unsigned int lBuf);


/* Logging Net Interface 
**
*/
extern	int 	SLog_NetEnable(_t_log_ctrl * pCtrl);
extern	int 	SLog_NetDisable(_t_log_ctrl * pCtrl);
extern	int 	SLog_NetSetIPPort(_t_log_ctrl * pCtrl, unsigned int ip, unsigned short port);

extern	int 	SLog_NetTx(_t_log_ctrl * pCtrl, unsigned char * pData, int lData);
extern	int 	SLog_NetRx(_t_log_ctrl * pCtrl, _t_cbentry * pCbEntry);

#ifdef __cplusplus
}
#endif

#endif


